const common = require('./base/common');

/**
 * 643. 子数组最大平均数 I
 * 
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findMaxAverage = function(nums, k) {
    // 先将窗口填满
    let avg = 0;
    for(let i=0; i < k; ++i) {
        avg += nums[i] / k;
    }

    // 再滑动窗口中计算最大值
    let maxAvg = avg;
    for(let i=k; i < nums.length; ++i) {
        avg -= (nums[i - k] - nums[i]) / k;
        maxAvg = Math.max(avg, maxAvg);
    }

    return maxAvg;
};

common.testing([
    [[1,12,-5,-6,50,3], 4],
    [[-5,3,4,7,9,12,8,8,1,1,0,-27,-50,102,3,-22,1,-29,-8,56,33,-2,1,7,9,10,23,88,109,-239,-333,-999,998], 6]
], [12.75, 41])(x => findMaxAverage(x[0], x[1]));